package com.rew.canteenchefs.managers;

import com.rew.canteenchefs.MyApplication;
import com.rew.canteenchefs.managers.imp.EUserManager;
import com.rew.canteenchefs.models.EUser;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.transaction.annotation.Transactional;

import static com.rew.canteenchefs.enumerate.ResultTypeEnum.SUCCESS;
import static org.junit.Assert.assertEquals;


/**
 * UserManager层测试.
 * 使用测试驱动开发
 *
 * Created by HuiWen Ren on 2016/12/2.
 */

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MyApplication.class)
@WebAppConfiguration
public class EUserManagerTset {

    @Autowired
    EUserManager eUserManager;

    private EUser eUser = new EUser();

    private static final String testID = "TSP001";
    private static final String testName = "user-test";
    private static final String testPassword = "SSS123";
    private static final String testNewPSD = "xxx123";

    @Before
    public void init(){
        eUser.setName(testName);
        eUser.setCard_id(testID);
        eUser.setPassword(testPassword);
    }

    @Test
    @Transactional
    public void should_return_right_when_insert_by_userDao(){
        assertEquals(SUCCESS.getValue(), eUserManager.insertUser(eUser).getCode(), 0);
    }

    @Test
    @Transactional
    public void should_return_right_PSD_after_update_user_info(){
        eUserManager.insertUser(eUser);
        eUser.setPassword(testNewPSD);
        assertEquals(SUCCESS.getDescription(),eUserManager.updateUser(eUser).getMessage());
        assertEquals(testNewPSD, eUserManager.selectUser(testID).getBean().getPassword());
    }

    @Test
    @Transactional
    public void should_return_right_when_delete_a_user(){
        eUserManager.insertUser(eUser);
        assertEquals(SUCCESS.getValue(),eUserManager.deleteUser(testID).getCode());
    }
}
